package com.itheima.travel.interceptors;

import com.itheima.travel.constant.Constant;
import com.itheima.travel.session.SubjectUserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//解决跨域问题，有多个服务器，但是浏览器请求的端口不一定相同
//而出现跨域问题
public class CrossInterceptor implements HandlerInterceptor {

    //将用户信息注入
    @Autowired
    private SubjectUserContext subjectUserContext;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //解决跨域
        String origin=request.getHeader("Origin");
        response.setHeader("Access-Control-Allow-Origin",origin);
        //设置方法
        response.setHeader("access-control-allow-methods", "*");
        //是否支持cookie跨域
        response.setHeader("access-control-allow-credentials","true");
        //指定头部跨域传递access Token
        response.setHeader("Access-Control-Allow-Headers", "Content-Type,"+ Constant.ACCESS_TOKEN);
        return true;
    }
}
